/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesoDatos;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import logicaNegocio.Maquina;
import oracle.jdbc.driver.OracleTypes;

/**
 *
 *
 */
public class GestorMaquina extends Conexion implements Gestor<Maquina> {
    private static final String ingresarMaquina="{call INSERTAR_MAQUINA(?)}";
    private static final String consultarMaquina="{?=call CONSULTAR_MAQUINA(?)}";
    private static final String eliminarMaquina="";
    private static final String modificarMaquina="{call MODIFICAR_ESTADO_MAQUINA(?,?)}";
    private static final String listarMaquina="{?=call LISTAR_MAQUINA}";

    @Override
    public void ingresarDato(Maquina clase) {
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(ingresarMaquina);
            cs.setInt(1, clase.getNumeroMaquina());
            boolean resultado = cs.execute();
            if(resultado){
                throw null;
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void eliminarDato(String identificacion) {
        CallableStatement cs = null;
        try{
            if(!this.Conectar())
                return;
            cs = this.conexion.prepareCall(eliminarMaquina);
            cs.setInt(1, Integer.parseInt(identificacion));
            int resultado = cs.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void modificarDato(Maquina clase) {
        CallableStatement cs = null;
        try{
            if(!this.Conectar())
                return;
            cs = this.conexion.prepareCall(modificarMaquina);
            cs.setInt(1, clase.getNumeroMaquina());
            cs.setInt(2, clase.getEstado());
            int resultado = cs.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public ArrayList<Maquina> listarDato() {
        CallableStatement cs = null;
        ArrayList<Maquina> coleccion = new ArrayList<Maquina>();
        Maquina maq = null;
        ResultSet rs = null;
        try{
            if(!this.Conectar())
                return null;
            cs = this.conexion.prepareCall(listarMaquina);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                maq = new Maquina(
                        rs.getInt(1),
                        rs.getInt(2)
                        );
                coleccion.add(maq);
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                this.Desconectar();
                if(coleccion == null || coleccion.isEmpty())
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
        }
        return coleccion;
    }

    @Override
    public Maquina consultarDato(String identificacion) {
        ResultSet rs = null;
        int id = Integer.parseInt(identificacion);
        Maquina maquina = null;
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(consultarMaquina);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setInt(2, id);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                maquina = new Maquina(
                        rs.getInt(1),
                        rs.getInt(2)
                        );
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                Desconectar();
                if(maquina == null)
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
        }
        return maquina;
    }
}
